package com.pig4cloud.pig.module.controller.wx;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.mer.Mer;
import com.pig4cloud.pig.module.api.entity.wx.UserMp;
import com.pig4cloud.pig.module.api.entity.wx.WxUser;
import com.pig4cloud.pig.module.service.mer.IMerService;
import com.pig4cloud.pig.module.service.wx.IUserMpService;
import com.pig4cloud.pig.module.service.wx.IWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 公众号关注的用户列Controller
 *
 * @author ruoyi
 * @date 2021-09-30
 */
@Validated
@Api(value = "公众号关注的用户列控制器", tags = {"公众号关注的用户列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mp/user")
public class UserMpController {

    private final IUserMpService userMpService;
    private final IMerService merService;
    private final IWxUserService wxUserService;

    /**
     * 查询公众号关注的用户列列表
     */
    @ApiOperation("查询公众号关注的用户列表")
    @GetMapping("/list")
    public TableDataInfo<UserMp> list(@Validated UserMp userMp) {
        QueryWrapper<UserMp> qw = new QueryWrapper<>();
        if(userMp.getMerId()!=null){
            qw.eq("mer_id",userMp.getMerId());
        }
        if(StrUtil.isNotEmpty(userMp.getNickName())){
            qw.like("nick_name",userMp.getNickName());
        }

        TableDataInfo<UserMp> td = PageUtils.buildDataInfo(userMpService.page(PageUtils.buildPage(),qw));
        List<UserMp> list = td.getData();
        list.forEach(user->{
            Mer mer = merService.getById(user.getMerId());
            if(mer!=null){
                user.setShopName(mer.getShopName());
            }
        });
        return td;
    }


    /**
     * 查询微信端用户列表
     */
    @ApiOperation("查询公众号关注用户列表")
    @GetMapping("/listBySearch")
    public R listByPhone(@Validated UserMp userMp) {
        QueryWrapper<UserMp> qw = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(userMp.getNickName())){
            qw.like("nick_name",userMp.getNickName());
        }
        qw.orderByDesc("create_time");
        List<UserMp> list = userMpService.list(qw);
        return R.ok(list);
    }

    /**
     * 导出公众号关注的用户列列表
     */
    @ApiOperation("导出公众号关注的用户列列表")
    @SysLog("公众号关注的用户列")
    @GetMapping("/export")
    public List export(@Validated UserMp userMp) {
        List<UserMp> list = userMpService.list();
        // ExcelUtil<UserMp> util = new // ExcelUtil<>(UserMp.class);
        return list;// util.exportExcel(list, "公众号关注的用户列");
    }

    /**
     * 获取公众号关注的用户列详细信息
     */
    @ApiOperation("获取公众号关注的用户列详细信息")
    @GetMapping("/{id}")
    public R<UserMp> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        UserMp userMp = userMpService.getById(id);
        return R.ok(userMp);
    }

    /**
     * 新增公众号关注的用户列
     */
    @ApiOperation("新增公众号关注的用户列")
    @SysLog("公众号关注的用户列")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody UserMp userMp) {
        return R.okByRow(userMpService.save(userMp) ? 1 : 0);
    }

    /**
     * 修改公众号关注的用户列
     */
    @ApiOperation("修改公众号关注的用户列")
    @SysLog("公众号关注的用户列")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody UserMp userMp) {
        UserMp mp = userMpService.getById(userMp.getId());
        if(mp != null){
            WxUser wxUser = wxUserService.selectByUniqueColumn("union_id",mp.getUnionId());
            if(wxUser != null){
                wxUser.setUserType(1);
                wxUserService.updateById(wxUser);
            }
        }
        return R.okByRow(userMpService.updateById(userMp) ? 1 : 0);
    }

    /**
     * 删除公众号关注的用户列
     */
    @ApiOperation("删除公众号关注的用户列")
    @SysLog("公众号关注的用户列")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(userMpService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
